//
//  HPClassView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/15.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "HPSelectedClassView.h"

@interface HPSelectedClassView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *picker;
@property (strong, nonatomic) NSMutableArray<NSString *> *data;
@property (assign, nonatomic) int selectValue;   //选择的值
@end

@implementation HPSelectedClassView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, 300, 300);
    [self setupCalendarView];
    // Do any additional setup after loading the view.
}
- (void)setupCalendarView {
    [self.view addSubview:self.picker];
    [self.view addSubview:self.confirmBtn];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@0).offset(-20);
        make.height.equalTo(@40);
        make.bottom.equalTo(@0).offset(-10);
    }];
}
//选择器的初始化和布局
- (UIPickerView *)picker{
    if (!_picker) {
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(30,0, 280, 300)];
        _picker.showsSelectionIndicator = YES;
        //设置代理
        _picker.delegate =self;
        _picker.dataSource =self;
    }
    return _picker;
}
//选择器数据的加载，从设定的最小日期到当前月
- (NSMutableArray<NSString *> *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
        //循环获取可选月份，从当前月份到最小月份
        for (int i=0; i<_dataArray.count; i++) {
            TeacherClassListModel *model = _dataArray[i];
            [_data addObject:model.name];
        }
    }
    return _data;
}
- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@"确定" forState:UIControlStateNormal];
        [sender setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(120, 40)] forState:UIControlStateNormal];
        sender.layer.cornerRadius = 4;
        sender.titleLabel.font = [UIFont systemFontOfSize:14];
        sender.layer.masksToBounds = YES;
        [sender addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventAllEvents];
        _confirmBtn = sender;
    }
    return _confirmBtn;
}
#pragma mark - UIPickerView的数据和布局，和tableview类似
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.data.count;
}

//每一行的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.data[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        pickerLabel.font = [UIFont systemFontOfSize:16];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
    
}
//选中时的效果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectValue = (int)row;
}

//返回高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35.0f;
}

//返回宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 300*fw;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    //    [self dismissWithAnimation:NO];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口

}
#pragma mark - 确定按钮
- (void)confirmClick:(UIButton *)sender{
    self.block(self.selectValue);
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
